package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.dto.*;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.mapper.SysRoleMapper;
import com.itheima.ydd.service.SysMenuService;
import com.itheima.ydd.service.SysRoleMenuService;
import com.itheima.ydd.service.SysRoleService;
import com.itheima.ydd.service.SysUserService;
import com.itheima.ydd.utils.TextTransUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class SysRoleServicerImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMenuService service;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public Result delSysRole(Integer id) {
        //条件对象
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRole::getId, id);
        String roleName = "";
        SysRole byId = this.getById(id);
        String name = this.getById(roleName).getName();
        //如果不是超级管理员,不让删除
        if ("superadmin".equals(name)) {
            return new Result(0, "对不起,您的权限不足");
        }
        this.removeById(id);
        return new Result(0, "");
    }

    /**
     * 产看全部
     *
     * @param page
     * @param limit
     * @param name
     * @return
     */
    @Override
    public Result findAll(String name, String page, String limit) {
        int pageI = Integer.parseInt(page);
        int limitI = Integer.parseInt(limit);

        PageDto<SysRole> sysRolePage = new PageDto<>(pageI, limitI);
        PageDto<SysRoleMenuDto> sysRoleDtoPage = new PageDto<>();

        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(name), SysRole::getName, name);
        this.page(sysRolePage, wrapper);

        //复制
        BeanUtils.copyProperties(sysRolePage, sysRoleDtoPage, "records");
        List<SysRole> sysRoleList = sysRolePage.getdata();
        List<SysRoleMenuDto> sysRoleDtosList = new ArrayList<>();

        for (SysRole sysRole : sysRoleList) {
            SysRoleMenuDto sysRoleDto = new SysRoleMenuDto();
            BeanUtils.copyProperties(sysRole, sysRoleDto);
            //去分支表中进行查找
            sysRoleDto.setRole_id(sysRole.getId());
            LambdaQueryWrapper<SysRoleMenu> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SysRoleMenu::getRole_id, sysRole.getId());
            List<SysRoleMenu> menuNames = service.list(wrapper1);
            String menuname = "";
            //对菜单id进行拼接
            for (int i = 0; i < menuNames.size(); i++) {
                //  1  1,2  1,2,3
                if (i == menuNames.size() - 1) {
                    menuname += menuNames.get(i).getMenu_id();
                } else {
                    menuname += menuNames.get(i).getMenu_id() + ",";
                }
            }
            sysRoleDto.setMenu_id(menuname);
            sysRoleDtosList.add(sysRoleDto);
        }

        sysRoleDtoPage.setRecords(sysRoleDtosList);
        return new Result(0, "", sysRoleDtoPage);
    }

    /**
     * 得到信息
     *
     * @param id
     */
    @Override
    public SysRoleDtoUtil getSysRoleInfo(String id) {
        //1.根据id找到所对应的回调信息
        SysRole sysRole = this.getById(id);
        SysRoleDtoUtil rollSysRole = new SysRoleDtoUtil();
        BeanUtils.copyProperties(sysRole, rollSysRole);
        //2.根据回调id去到role_menu表中查找对应的菜单集合
        LambdaQueryWrapper<SysRoleMenu> srmWrapper = new LambdaQueryWrapper<>();
        srmWrapper.eq(SysRoleMenu::getRole_id, sysRole.getId());
        List<SysRoleMenu> sysRoleMenus = service.list(srmWrapper);
        //3.根据遍历集合去判断一级菜单
        for (SysRoleMenu sysRoleMenu : sysRoleMenus) {
            //得到menuId
            //根据id去menu表中进行匹配查找
            //3.1一级判断---pid=0&&type=1
            LambdaQueryWrapper<SysMenu> sysMenuWra = new LambdaQueryWrapper<>();
            sysMenuWra.eq(SysMenu::getId, 0).eq(SysMenu::getType, 1);
            //一级菜单
            List<SysMenu> sysMenusParents = sysMenuService.list(sysMenuWra);
        }
        //3.2二级判断---pid=一级id&&type=1
        //3.3按钮判断---pid=二级id&&type=2
        //4.封装数据
        return null;
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Result deleteSysRole(String id) {
        //1.判断用户表中有没有相应的关联数据
        //将转化为文本格式
        String roleId = TextTransUtil.doArrayToString(id);
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getRoleId, roleId);
        List<SysUser> sysUsers = sysUserService.list(wrapper);
        if (sysUsers.size()>0){
            return new Result(300,"对不起,该角色下面有多个用户,不能删除");
        }
        //要是删除就删两个表---1.role  2.角色和menu关联表
        //先利用roleId去关联表中删除对应在字段
        LambdaQueryWrapper<SysRoleMenu> sysRoleMenuWrapper = new LambdaQueryWrapper<>();
        sysRoleMenuWrapper.eq(SysRoleMenu::getRole_id, id);
        boolean falg = service.remove(sysRoleMenuWrapper);
        //删除role表中对应数据
        boolean removeFalg = this.removeById(id);
        if (falg&&removeFalg){
            return new Result(0,"删除成功");
        }
        return new Result(300,"对不起,删除失败");
    }

    /**
     * 添加
     */
    @Override
    @Transactional
    public Result addRole(Map<String, Object> map) {
        //1.将得到的map进行数据的转化
        String name = (String) map.get("name");
        String description = (String) map.get("description");
        List<Integer> menuids = (List<Integer>) map.get("menuids");
        //2.进行数据的转换
        SysRole sysRole = new SysRole();
        sysRole.setName(name);
        sysRole.setDescription(description);
        //3.对sysrole进行添加
        this.save(sysRole);
        //4.对菜单进行
        //对集合进行遍历,得到对象集合
        List<SysRoleMenu> sysRoleMenus = new ArrayList<>();
        for (Integer menuid : menuids) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRole_id(sysRole.getId());
            sysRoleMenu.setMenu_id(menuid + "");
            sysRoleMenus.add(sysRoleMenu);
        }
        boolean falg = service.saveBatch(sysRoleMenus);
        return new Result(0, falg ? "添加成功" : "添加失败");
    }
}
